package heyuanyuan;

import java.util.Scanner;

public class HospitalQueueSystem {
    private CircularQueue patientQueue;
    private Scanner scanner;

    public HospitalQueueSystem(int capacity) {
        this.patientQueue = new CircularQueue(capacity);
        this.scanner = new Scanner(System.in);
    }

    private void displayMenu() {
        System.out.println("\n===== 医院就诊排队系统 =====");
        System.out.println("1. 排队（病人到达）");
        System.out.println("2. 就诊（护士叫号）");
        System.out.println("3. 查看排队情况");
        System.out.println("4. 不再排队，余下顺序就诊");
        System.out.println("5. 下班");
        System.out.print("请选择操作(1-5): ");
    }

    public void run() {
        while (true) {
            displayMenu();
            String input = scanner.nextLine();

            try {
                int choice = Integer.parseInt(input);

                switch (choice) {
                    case 1:
                        addPatient();
                        break;
                    case 2:
                        servePatient();
                        break;
                    case 3:
                        viewQueue();
                        break;
                    case 4:
                        finishQueue();
                        return;
                    case 5:
                        closeHospital();
                        return;
                    default:
                        System.out.println("无效选择，请重新输入(1-5)");
                }
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的数字(1-5)");
            }
        }
    }

    private void addPatient() {
        System.out.print("请输入病人病历号: ");
        String medicalRecord = scanner.nextLine();

        if (medicalRecord.isEmpty()) {
            System.out.println("病历号不能为空");
            return;
        }

        if (patientQueue.enqueue(medicalRecord)) {
            System.out.println("病人 " + medicalRecord + " 已加入排队");
        } else {
            System.out.println("排队已满，无法接收更多病人");
        }
    }

    private void servePatient() {
        String patient = patientQueue.dequeue();
        if (patient != null) {
            System.out.println("请病人 " + patient + " 进入诊室就诊");
        } else {
            System.out.println("当前没有病人在排队");
        }
    }

    private void viewQueue() {
        System.out.println("当前排队情况:");
        patientQueue.display();
    }

    private void finishQueue() {
        System.out.println("停止接收新病人，余下病人按顺序就诊:");
        viewQueue();
        System.out.println("系统将退出...");
    }

    private void closeHospital() {
        if (!patientQueue.isEmpty()) {
            System.out.println("以下病人未就诊，请明天再来:");
            viewQueue();
        } else {
            System.out.println("所有病人都已就诊完毕");
        }
        System.out.println("医院已下班");
    }

    public static void main(String[] args) {
        System.out.println("===== 医院就诊排队系统启动 =====");
        Scanner initScanner = new Scanner(System.in);
        int capacity = 0;

        while (true) {
            System.out.print("请输入诊室最大排队容量(正整数): ");
            String input = initScanner.nextLine();

            try {
                capacity = Integer.parseInt(input);
                if (capacity > 0) {
                    break;
                } else {
                    System.out.println("请输入大于0的正整数");
                }
            } catch (NumberFormatException e) {
                System.out.println("请输入有效的数字");
            }
        }

        HospitalQueueSystem system = new HospitalQueueSystem(capacity);
        system.run();
        initScanner.close();
    }
}

class CircularQueue {
    private String[] queue;
    private int front;
    private int rear;
    private int capacity;

    public CircularQueue(int size) {
        this.capacity = size + 1; // 多一个空间用于判断队列满
        this.queue = new String[this.capacity];
        this.front = 0;
        this.rear = 0;
    }

    public boolean isEmpty() {
        return front == rear;
    }

    public boolean isFull() {
        return (rear + 1) % capacity == front;
    }

    public boolean enqueue(String item) {
        if (isFull()) {
            return false;
        }
        queue[rear] = item;
        rear = (rear + 1) % capacity;
        return true;
    }

    public String dequeue() {
        if (isEmpty()) {
            return null;
        }
        String item = queue[front];
        front = (front + 1) % capacity;
        return item;
    }

    public void display() {
        if (isEmpty()) {
            System.out.println("当前没有病人在排队");
            return;
        }
        int i = front;
        while (i != rear) {
            System.out.println("病历号: " + queue[i]);
            i = (i + 1) % capacity;
        }
    }
}